package com.banshuibao.bsb_android.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * Created by wangpan on 2017/9/22.
 */

public class HorizontalDelete extends HorizontalScrollView {

    public static final float radio = 0.3f;//菜单占屏幕的30%
    public int greenWidth;
    public int menuWidth;
    private boolean once = true;
    private boolean isOpen;
    private long downTime = 0;

    private HorizontalDeleteMoveListener listener;

    public void setListener(HorizontalDeleteMoveListener listener) {
        this.listener = listener;
    }

    public interface HorizontalDeleteMoveListener {
        void open(HorizontalDelete horizontalDelete);

        void close();
    }

    public HorizontalDelete(Context context, AttributeSet attrs) {
        super(context, attrs);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        greenWidth = outMetrics.widthPixels;
        menuWidth = (int) (greenWidth * radio);
        setOverScrollMode(View.OVER_SCROLL_NEVER);
        setHorizontalScrollBarEnabled(false);
    }

    public HorizontalDelete(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //关闭
    public void closeMenu() {
        this.smoothScrollTo(0, 0);
        isOpen = false;
    }

    //是否关闭菜单
    public boolean isOpen() {
        return isOpen;
    }

    //记录打开菜单的view
    public void openMenu() {
        View view = this;
        while (true) {
            view = (View) view.getParent();
            if (view instanceof ListView) {
                break;
            }
        }
        if (listener != null)
            listener.open(this);
        isOpen = true;
    }

    //关闭上次记录打开菜单的view
    public void coloseMenu() {
        if (!isOpen) {
            View view = this;
            while (true) {
                view = (View) view.getParent();
                if (view instanceof ListView) {
                    break;
                }
            }
            if (listener != null)
                listener.close();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (once) {
            LinearLayout linearLayout = (LinearLayout) getChildAt(0);
            linearLayout.getChildAt(0).getLayoutParams().width = greenWidth;
            linearLayout.getChildAt(1).getLayoutParams().width = menuWidth;
            once = false;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                int x = getScrollX();
                if (System.currentTimeMillis() - downTime <= 100 && x == 0) {
                    if (mCustomOnClickListener != null) {
                        mCustomOnClickListener.onClick();
                    }
                    return false;
                }
                if (Math.abs(x) > menuWidth / 2) {
                    this.smoothScrollTo(menuWidth, 0);
                    openMenu();
                } else {
                    this.smoothScrollTo(0, 0);
                }
                return true;
            case MotionEvent.ACTION_DOWN:
                downTime = System.currentTimeMillis();
                coloseMenu();
                break;

        }
        return super.onTouchEvent(ev);
    }

    public interface CustomOnClickListener {
        void onClick();
    }

    public CustomOnClickListener mCustomOnClickListener;

    public void setCustomOnClickListener(CustomOnClickListener listener) {
        this.mCustomOnClickListener = listener;
    }
}
